package com.example.jetpack_databinding.adapter

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide

class ImageViewBindingAdapter {
    companion object {
        //加载网络图片
        @BindingAdapter("image")
        @JvmStatic
        fun setImage(imageView: ImageView, url: String) {
            if (!TextUtils.isEmpty(url)) {
                Log.d("ImageViewBindingAdapter", "url:${url}")
            } else {
                Log.d("ImageViewBindingAdapter", "url为空")
            }
        }


        //加载本地图片
        @BindingAdapter("image")
        @JvmStatic
        fun setLocalImage(imageView: ImageView, resId: Int) {
            imageView.setImageResource(resId)
        }

        //加载网络图片,没有则加载本地图片
        @BindingAdapter(value = ["image", "defaultImageResource"], requireAll = false)
        @JvmStatic
        fun setImage(imageView: ImageView, url: String?, resId: Int?) {
            if (!TextUtils.isEmpty(url)) {
                Log.d("ImageViewBindingAdapter", "url:${url}")
                Glide.with(imageView.context).load(url).into(imageView)
            } else {
                Log.d("ImageViewBindingAdapter", "url为空2222")
                resId?.let { imageView.setImageResource(it) }
            }
        }
    }

}